package com.ncloud.billing.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.billing.entity.BsCycle;

@Mapper
public interface BsCycleMapper {

	@Select(value = {"SELECT cycle_id, begin_time, half_time, end_time, use_tag, act_tag, remark FROM bi_p_cycle where now() between begin_time and end_time "})
	@Results({ 
	       @Result(column = "cycle_id", property = "cycle_id"),@Result(column = "begin_time", property = "begin_time"),
	       @Result(column="half_time",property="half_time"),@Result(column="end_time",property="end_time"),
	       @Result(column="use_tag",property="use_tag"),@Result(column="act_tag",property="act_tag"),
	       @Result(column="remark",property="remark")})
	public List<BsCycle> getCurrentCycle();
	
	@Select(value = {"SELECT cycle_id, begin_time, half_time, end_time, use_tag, act_tag, remark FROM bi_p_cycle where use_tag=1 order by cycle_id desc limit 1 "})
	@Results({ 
	       @Result(column = "cycle_id", property = "cycle_id"),@Result(column = "begin_time", property = "begin_time"),
	       @Result(column="half_time",property="half_time"),@Result(column="end_time",property="end_time"),
	       @Result(column="use_tag",property="use_tag"),@Result(column="act_tag",property="act_tag"),
	       @Result(column="remark",property="remark")})
	public List<BsCycle> getMaxOpenCycle();
	
	@Select(value = {"SELECT cycle_id, begin_time, half_time, end_time, use_tag, act_tag, remark FROM bi_p_cycle where use_tag=1 and act_tag=0 order by cycle_id limit 1 "})
	@Results({ 
	       @Result(column = "cycle_id", property = "cycle_id"),@Result(column = "begin_time", property = "begin_time"),
	       @Result(column="half_time",property="half_time"),@Result(column="end_time",property="end_time"),
	       @Result(column="use_tag",property="use_tag"),@Result(column="act_tag",property="act_tag"),
	       @Result(column="remark",property="remark")})
	public List<BsCycle> getMinBillCycle();
	
	@Update(value = {"UPDATE bi_p_cycle SET use_tag = #{use_tag} WHERE cycle_id = #{cycle_id} "})
	public int updateCycleUseTag(BsCycle bsCycle);
	
	@Update(value = {"UPDATE bi_p_cycle SET act_tag = #{act_tag} WHERE cycle_id = #{cycle_id} "})
	public int updateCycleActTag(BsCycle bsCycle);
}
